package com.zzxy.ssm.Service.impl;

import com.zzxy.ssm.Service.IItemService;
import com.zzxy.ssm.entity.Item;
import com.zzxy.ssm.mapper.ItemMapper;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class ItemServiceImpl implements IItemService {


    private ItemMapper itemMapper;

    public ItemMapper getItemMapper() {
        return itemMapper;
    }

    public void setItemMapper(ItemMapper itemMapper) {
        this.itemMapper = itemMapper;
    }

    @Override
    public List<Item> findAll() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicaiontContext.xml");
         itemMapper = (ItemMapper) context.getBean("itemMapper");
        return itemMapper.findAllList();
    }

    @Override
    public Item findById(Integer id) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicaiontContext.xml");
        itemMapper = (ItemMapper) context.getBean("itemMapper");
        return itemMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Item item) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicaiontContext.xml");
         itemMapper = (ItemMapper) context.getBean("itemMapper");
         if (item.getId()==null){
             itemMapper.insert(item);
         }
        itemMapper.updateByPrimaryKey(item);
    }

    @Override
    public void update(Item item) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicaiontContext.xml");
        itemMapper = (ItemMapper) context.getBean("itemMapper");
        itemMapper.updateByPrimaryKey(item);
    }

    @Override
    public void deleteById(Integer id) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicaiontContext.xml");
        itemMapper = (ItemMapper) context.getBean("itemMapper");
        itemMapper.deleteByPrimaryKey(id);
    }
}
